package me.nibo.bignerdranch.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author nibo
 */
public class CheatActivity extends AppCompatActivity {
    public static final String EXTRA_ANSWER_IS_TRUE = "me.nibo.bignerdranch.geoquiz.answer_is_true";
    public static final String EXTRA_ANSWER_SHOWN = "me.nibo.bignerdranch.geoquiz.answer_shown";
    private static final String KEY_IS_CHEAT = "is_cheat";
    private static final String KEY_ANSWER_IS_TRUE = "answer_is_true";
    private boolean mAnswerIsTrue;
    private TextView mAnswerTextView;
    private boolean mIsCheat = false;
    private int answerIsTrueId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);
        if (savedInstanceState != null) {
            mIsCheat = savedInstanceState.getBoolean(KEY_IS_CHEAT, false);
            answerIsTrueId = savedInstanceState.getInt(KEY_ANSWER_IS_TRUE, 0);
        }
        mAnswerTextView = (TextView) findViewById(R.id.answerTextView);
        if (answerIsTrueId != 0) {
            mAnswerTextView.setText(answerIsTrueId);
        }
        mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
        Button mShowAnswerButton = (Button) findViewById(R.id.showAnswerButton);
        mShowAnswerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAnswerIsTrue) {
                    answerIsTrueId = R.string.true_button;
                    mAnswerTextView.setText(answerIsTrueId);
                } else {
                    answerIsTrueId = R.string.false_button;
                    mAnswerTextView.setText(answerIsTrueId);
                }
                mIsCheat = true;
                setAnswerShownResult(true);
            }
        });
        TextView mShowSDKLevel = (TextView) findViewById(R.id.showSDKLevelTextView);
        mShowSDKLevel.setText("API level " + Build.VERSION.SDK_INT);
        setAnswerShownResult(mIsCheat);
    }

    private void setAnswerShownResult(boolean isAnswerShown) {
        Intent intent = new Intent();
        intent.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
        setResult(Activity.RESULT_OK, intent);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(KEY_IS_CHEAT, mIsCheat);
        outState.putInt(KEY_ANSWER_IS_TRUE, answerIsTrueId);
    }
}
